﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSBusinessEntities;
using System.Transactions;
using MBSVO;
using MBSProcessComponent;
namespace MBSBLC
{
    public partial class BLC
    {
        #region ShowTime

        ShowTimeBE showTimeBE;

        public bool AddShowTime(int ShowID, double Fare, DateTime ShowDateTime, string SeatsAvailable, int MovieScheduleID, bool IsDeleted)
        {
            movieScheduleBE = (MovieScheduleBE)movieScheduleBE.GetByID(MovieScheduleID);
            if (movieScheduleBE.IsValid)
            {
                showTimeBE = new ShowTimeBE(ShowID, Fare, ShowDateTime, SeatsAvailable, MovieScheduleID, IsDeleted);
                return showTimeBE.Add();
            }

            return false;

        }

        public bool DeleteShowTime(int ShowTimeID)
        {
            showTimeBE = new ShowTimeBE();

            return showTimeBE.Delete(ShowTimeID);


        }

        private ShowTimeBE GetShowTimeBEByID(int ShowTimeID)
        {
            showTimeBE = new ShowTimeBE();
            showTimeBE = (ShowTimeBE)showTimeBE.GetByID(ShowTimeID);

            return showTimeBE;
        }

        public ShowTimeVO GetShowTimeByID(int ShowTimeID)
        {

            ShowTimeVO showTimeVO = BLCUtility.convertShowTimeBEtoVO(GetShowTimeBEByID(ShowTimeID));
            return showTimeVO;
        }

        public Dictionary<string, Dictionary<int, string>> GetShowTimeByMovieCinemaStartDateAndEndDate(int MovieID, int CinemaID, DateTime StartQueryDate, DateTime EndQueryDate)
        {

            ShowTimeBE showTimeBE = new ShowTimeBE();

            List<ShowTimeBE> showTimeBEList = showTimeBE.GetShowTimeByMovieCinemaStartDateAndEndDate(MovieID, CinemaID, StartQueryDate, EndQueryDate);
            Dictionary<string, Dictionary<int, string>> showTimeGroupDictionary = new Dictionary<string, Dictionary<int, string>>();
            foreach (ShowTimeBE ShowTimeBE in showTimeBEList)
            {
                string dateKey = ShowTimeBE.ShowDateTime.ToString("D");
                string showTimeDisplay = ShowTimeBE.ShowDateTime.ToString("HH:mmtt");
                Dictionary<int, string> showTimeDictionary;
                if (showTimeGroupDictionary.ContainsKey(dateKey))
                {
                    showTimeDictionary = showTimeGroupDictionary[dateKey];
                    showTimeGroupDictionary.Remove(dateKey);
                }
                else
                {
                    showTimeDictionary = new Dictionary<int, string>();
                }
                showTimeDictionary.Add(ShowTimeBE.ShowID, showTimeDisplay);
                showTimeGroupDictionary.Add(dateKey, showTimeDictionary);
            }

            return showTimeGroupDictionary;
        }

         public Dictionary<string, Dictionary<int, string>> GetShowTimeWithScreenNoByMovieCinemaStartDateAndEndDate(int MovieID, int CinemaID, DateTime StartQueryDate, DateTime EndQueryDate)
        {

            ShowTimeBE showTimeBE = new ShowTimeBE();

            List<KeyValuePair<ShowTimeBE, int>> showTimeBEPairList = showTimeBE.GetPairShowTimeByMovieCinemaStartDateAndEndDate(MovieID, CinemaID, StartQueryDate, EndQueryDate);
            Dictionary<string, Dictionary<int, string>> showTimeGroupDictionary = new Dictionary<string, Dictionary<int, string>>();
            foreach (KeyValuePair<ShowTimeBE, int> ShowTimeBEPair in showTimeBEPairList)
            {
                ShowTimeBE showTimeBETemp = ShowTimeBEPair.Key;
                int screenNo = ShowTimeBEPair.Value;
                string dateKey = showTimeBETemp.ShowDateTime.ToString("D");
                string showTimeDisplay = showTimeBETemp.ShowDateTime.ToString("HH:mmtt") + " (#" +screenNo + ")";
                Dictionary<int, string> showTimeDictionary;
                if (showTimeGroupDictionary.ContainsKey(dateKey))
                {
                    showTimeDictionary = showTimeGroupDictionary[dateKey];
                    showTimeGroupDictionary.Remove(dateKey);
                }
                else
                {
                    showTimeDictionary = new Dictionary<int, string>();
                }
                showTimeDictionary.Add(showTimeBETemp.ShowID, showTimeDisplay);
                showTimeGroupDictionary.Add(dateKey, showTimeDictionary);
            }

            return showTimeGroupDictionary;
        }
        

        public Dictionary<String, String> getShowTimeSeatDictionary(int ShowTimeId)
        {
            ShowTimeBE showTimeBE = GetShowTimeBEByID(ShowTimeId);
            Dictionary<int, Dictionary<String, String>> ShowTimeDictionary = MBSCache.getShowTimeDictionary();
            Dictionary<string, string> seatDictionary = new Dictionary<String, String>();

            if (showTimeBE == null) return seatDictionary;
            if (ShowTimeDictionary.Count == 0)
            {

                seatDictionary = BLCUtility.getSeatStatusDictionary(showTimeBE.SeatsAvailable);
                ShowTimeDictionary.Add(ShowTimeId, seatDictionary);
                MBSCache.setShowTimeDictionary(ShowTimeDictionary);
            }
            else
            {
                if (ShowTimeDictionary.ContainsKey(ShowTimeId))
                {
                    seatDictionary = ShowTimeDictionary[ShowTimeId];
                }
                else
                {
                    seatDictionary = BLCUtility.getSeatStatusDictionary(showTimeBE.SeatsAvailable);
                    ShowTimeDictionary.Add(ShowTimeId, seatDictionary);
                    //  MBSCache.setShowTimeDictionary(ShowTimeDictionary);
                }
            }
            return seatDictionary;
        }

        #endregion
    }
}
